<?php
/**
 * 用户
 * User: Acer
 * Date: 2022/2/3
 * Time: 20:49
 */
namespace app\admin\service;
use app\admin\models\UserModel as UserModel;
use app\card\service\CardProService;
use app\card\service\CardService;
use app\card\service\CardTypeService;
use app\move\models\FamilyCardModel;
use app\move\models\IdentityCardModel;
use app\move\models\IdentityCardBlacklistModel;
use app\move\service\CardUserService;
use app\move\service\WxUserService;

class UserServide{

    // 获取列表
    public function  s_user_list($page,$paginate = 10,$like){
        $UserModel = new UserModel();
        // 用户名称
        if(isset($like['username'])&& !empty($like['username'])){
            $UserModel->whereLike('username','%'.$like['username'].'%');
        }
        $Userlist = $UserModel
            ->alias('a')
            ->join('identity_card b','b.uid = a.id','LEFT');

        // 用户电话号码
        if(isset($like['mobile'])&& !empty($like['mobile'])){
            $Userlist->whereLike('b.mobile','%'.$like['mobile'].'%');
        }

        // 身份证
        if(isset($like['id_card'])&& !empty($like['id_card'])){
            $Userlist->whereLike('b.id_card','%'.$like['id_card'].'%');
        }

        $list = $Userlist->paginate($paginate,false,['page'=>$page]);
        return obj_to_arr($list);
    }

    // 重置用户密码
    public function s_reset_password($mobile){
        $UserModel = new UserModel();
        $uid = $UserModel->where('mobile',$mobile)->value('id');
        if(!$uid) json_err(-204,['该用户，']);
        // 重置密码
        $res = $UserModel->where('mobile',$mobile)->update(['password'=>md5(config('default_passd'))]);
        return $res;
    }

    // 根据用户id，获取卡片列表
    public function s_get_user_card_list($uid){
        $CardUserService = new CardUserService();
        $CardProService = new CardProService();
        $CardTypeService = new CardTypeService();
        $CardService = new CardService();
        $FamilyCardModel = new FamilyCardModel();
        $IdentityCardModel = new IdentityCardModel();
        // 获取激活的卡片[主卡]
         $cardUserlsit = $CardUserService->s_get_user_card($uid);
         if($cardUserlsit){
            foreach($cardUserlsit as $key=>$value){
                // 卡片产品信息
                $card_pro_info = $CardProService->m_card_pro_info($value['pid']);
                $cardUserlsit[$key]['card_pro_info'] = $card_pro_info;
                // 卡片信息
                $card_info = $CardService->getCardInfo($card_pro_info->cid);
                $cardUserlsit[$key]['card_info'] =$card_info;
                // 获取卡片类型
                $cardUserlsit[$key]['card_type_info'] = $CardTypeService->s_card_type_info($card_info->ct_id);
            }
         }
        // 获取卡片信息 [成员卡]
        $member_card_info = [];
        // 获取认证信息
        $IdentityCardInfo = $IdentityCardModel->where('uid',$uid)->find();
        if( !empty($IdentityCardInfo->did)){
            // 获取家庭信息
            $FamilyCard = $FamilyCardModel->where('did',$IdentityCardInfo->did)->select();
            if($FamilyCard){
                foreach($FamilyCard as $key=>$value){
                    // 卡片产品信息
                    $card_pro_info = $CardProService->m_card_pro_info($value['pid']);
                    $FamilyCard[$key]['card_pro_info'] = $card_pro_info;
                    // 卡片信息
                    $card_info = $CardService->getCardInfo($card_pro_info->cid);
                    $FamilyCard[$key]['card_info'] =$card_info;
                    // 获取卡片类型
                    $FamilyCard[$key]['card_type_info'] = $CardTypeService->s_card_type_info($card_info->ct_id);
                }
            }
            $member_card_info = $FamilyCard;
        }
        return [
            'member_card_info' => $member_card_info, // 成员卡信息
            'card_info' =>  $cardUserlsit // 主卡信息
        ];
    }
    // 根据用户id，获取用户信息
    public function s_get_user_info($uid){
       $UserModel = new UserModel();
       return $UserModel->where('id',$uid)->find();
    }
    // 修改用户信息
    public function s_modify_user($data){
        $WxUserService = new WxUserService();
        // 身份证号是否正确
        if(!isIdno($data['id_card'])) json_err(-210,['身份证号不正确！']);
        // 校验身份证是否已存在
        $IdentityCardModel = new IdentityCardModel();
        $test_id_card = $IdentityCardModel->where('did','<>',$data['did'])->where('id_card',$data['id_card'])->value("id_card");
        if($test_id_card) json_err(-210,['填写的身份证号已被他人使用！']);
        $test_mobile= $IdentityCardModel->where('did','<>',$data['did'])->where('mobile',$data['mobile'])->value("mobile");
        if($test_mobile) json_err(-210,['填写的电话号码已被他人使用！']);
        $idenAuthenticationData = $WxUserService->s_idenAuthentication($data['id_card'],$data['name']);
        if((int)$idenAuthenticationData['respCode'] !== 0000) json_err(-210,[$idenAuthenticationData['respMessage']]);
        // 通过身份证号查询出出生日期
        $birth = strlen($data['id_card'])==15 ? ('19' . substr($data['id_card'], 6, 6)) : substr($data['id_card'], 6, 8);
        $birthday = substr($birth,0,4).'-'. substr($birth,4,2).'-'. substr($birth,6,2);
        // 通过身份证号查询出性别
        $sex= $WxUserService->get_sex($data['id_card']);
        $userInfo = [
            'name'      =>  $data['name'],
            'id_card'   =>  $data['id_card'],
            'birth'     =>  $birthday,
            'sex'       =>  $sex,
            'mobile'    =>  $data['mobile'],
            'province'  =>  $idenAuthenticationData['province'],
            'county'    =>  $idenAuthenticationData['county'],
            'city'      =>  $idenAuthenticationData['city'],
            'age'       =>  $idenAuthenticationData['age']
        ];
        $IdentityCardModel->where('did',$data['did'])->update($userInfo);

        // 修改家庭卡成员关系
        if(!empty($data['fid']) && !empty($data['type'])){
            $FamilyCardModel = new FamilyCardModel();
            $FamilyCardModel->where('fid',$data['fid'])->update(['type'=>$data['type']]);
        }
        return true;
    }



      // 根据用户id，删除用户信息
      public function del_user($uid){
	(new IdentityCardModel())->where('uid',$uid)->delete();
        return (new UserModel())->where('id',$uid)->delete();
     }


    // 添加黑名单
    public function add_blacklist($data) {
        if(!isIdno($data['id_card'])) json_err(-210,['身份证号不正确！']);
        $IdentityCardBlacklistModel = new IdentityCardBlacklistModel();
        $IdentityCardBlacklistModel->insert($data);
        return true;
    }

    // 黑名单列表
    public function blacklist($page,$paginate = 10,$like) {
        $IdentityCardBlacklistModel = new IdentityCardBlacklistModel();
        // 身份证
        if(isset($like['id_card'])&& !empty($like['id_card'])){
            $IdentityCardBlacklistModel->whereLike('id_card','%'.$like['id_card'].'%');
        }
        $list = $IdentityCardBlacklistModel->paginate($paginate,false,['page'=>$page]);
        return obj_to_arr($list);
    }

    // 修改黑名单
    public function modify_blacklist($data) {
        if(!isIdno($data['id_card'])) json_err(-210,['身份证号不正确！']);
        $IdentityCardBlacklistModel = new IdentityCardBlacklistModel();
        $IdentityCardBlacklistModel->where('did',$data['did'])->update($data);
        return true;
    }

    // 删除黑名单
    public function delete_blacklist($data) {
        $IdentityCardBlacklistModel = new IdentityCardBlacklistModel();
        $IdentityCardBlacklistModel->where('did',$data['did'])->delete();
        return true;
    }

}
